/*
 *  Copyright (C) 2010 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.balisunrise.nbpam.windows;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import org.balisunrise.annotation.ActionMap;
import org.balisunrise.annotation.VisualAction;
import org.balisunrise.annotation.VisualActionTarget;
import org.balisunrise.annotation.util.AnnotationsUtilitiesException;
import org.balisunrise.application.Application;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.Transaction;
import org.balisunrise.common.TransactionActions;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.netbeans.api.settings.ConvertAsProperties;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.balisunrise.nbpam.view//Transaction//EN",
autostore = false)
@ActionMap(classes=TransactionTopComponent.class)
public final class TransactionTopComponent 
        extends AbstractTopComponentView<Transaction<?,?,?,JPanel>> {

    public TransactionTopComponent(){
        super();
    }

    public TransactionTopComponent(Transaction<?,?,?,JPanel> transaction){
        super(transaction);

        try{
            init();
        }catch (Exception x){
            Application.get().getLogger().log(Level.SEVERE,
                    "Falha ao inicar TransactionTopComponent", x);
        }
    }

    @VisualAction(alias="",
        icon="BACK1",
        index=Integer.MAX_VALUE,
        text="Sair",
        toolTipText="Fechar a Tela",
        focusable=false,
        target=VisualActionTarget.TRANSACTION)
    public void sair(){
        this.close();
    }

    private void init() throws AnnotationsUtilitiesException{
        try{
            List<org.balisunrise.common.VisualAction> actions =
                    Application.get()
                    .getVisualEntityManager()
                    .getActionBuilder()
                    .createVisualAction(this, "");

            if(actions.size() > 0){
                TransactionActions ta = visualEntity.getTransactionAction();
                org.balisunrise.common.VisualAction va = actions.get(0);
                va.setOwner(ta);
                ta.addAction(actions.get(0));
            }
        }catch(BuilderException bex){
            String msg = "Falha ao inicial TransactionDialog!";
            Application.get().getLogger().log(Level.SEVERE, msg, bex);
        }
        visualEntity.showSearch();
    }

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized TransactionTopComponent getDefault() {
        if (instance == null || !(instance instanceof TransactionTopComponent))
            instance = new TransactionTopComponent();

        return (TransactionTopComponent) instance;
    }

    /**
     * Obtain the TransactionTopComponent instance.
     * Never call {@link #getDefault} directly!
     */
    public static synchronized TransactionTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(TransactionTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof TransactionTopComponent) {
            return (TransactionTopComponent) win;
        }
        Logger.getLogger(TransactionTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public void writeProperties(java.util.Properties p) {
        p.setProperty("version", "1.0");
    }

    @Override
    public Object readProperties(java.util.Properties p) {
        if (instance == null) {
            instance = this;
        }
        instance.readPropertiesImpl(p);
        return instance;
    }
}
